package com.tulele.vtool.task.async;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.tulele.vtool.mapper.KeywordDetailLogMapper;
import com.tulele.vtool.model.BrandInfo;
import com.tulele.vtool.model.PlatformSysInfo;
import com.tulele.vtool.model.UserInfo;
import com.tulele.vtool.model.logEntity.KeyWrodInfo;
import com.tulele.vtool.service.IArticleInfoService;
import com.tulele.vtool.service.IUserBrandInfoService;
import com.tulele.vtool.utils.DateUtil;
import com.tulele.vtool.utils.HttpClientUtils;
import com.tulele.vtool.utils.RedisCacheUtil;
import com.tulele.vtool.wechat.service.WeixinService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class SendkefuMsgAsyncTask {

    @Autowired
    private IArticleInfoService articleInfoService;

    @Autowired
    private KeywordDetailLogMapper keywordDetailLogMapper;

    @Autowired
    private RedisCacheUtil redisCacheUtil;

    @Autowired
    private IUserBrandInfoService userBrandInfoService;


    public void sendAttentionRemindKefuMsg(String userId, PlatformSysInfo platformSysInfoConfig) {

        log.info("[enter sendAttentionRemindKefuMsg]**** userId= " + userId);

        try {
            Map<String, String> headMap = Maps.newHashMap();
            headMap.put("userId", userId);
            headMap.put("profile", platformSysInfoConfig.getSysGzh().getGzhCode());

            String finalReqUrl = platformSysInfoConfig.getTaskUrl() + "callFifthTime";

            String wxSpdiderResult = HttpClientUtils.sendPost(finalReqUrl, headMap, Maps.newHashMap());

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void sendVideoKefuMsg(String userId, PlatformSysInfo platformSysInfoConfig) {

        log.info("[enter sendVideoKefuMsg]**** userId= " + userId);

        try {
            Map<String, String> headMap = Maps.newHashMap();
            headMap.put("userId", userId);
            headMap.put("profile", platformSysInfoConfig.getSysGzh().getGzhCode());

            String finalReqUrl = platformSysInfoConfig.getTaskUrl() + "callVideo";

            String wxSpdiderResult = HttpClientUtils.sendPost(finalReqUrl, headMap, Maps.newHashMap());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void send10secKefuMsg(UserInfo userInfo, PlatformSysInfo platformSysInfoConfig, long millis) {

        try {
            Thread.sleep(millis);
            if (userInfo != null) {
                int h = 24;
                KeyWrodInfo keyWrodInfo = keywordDetailLogMapper.getLastKeyWord(platformSysInfoConfig.getSysGzh().getGzhCode(), userInfo.getUserId());

                if (keyWrodInfo != null) {
                    h = DateUtil.getTimeDiff(3, keyWrodInfo.getCreateTime());
                }

                Object activeMsgPush = redisCacheUtil.getCacheObject("ACTIVE_10SEC_MSG_PUSH" + userInfo.getUserId());

                if (activeMsgPush == null && h >= 24) {//一个小时内没发过，而且 距离上一次 输入回复已经有24小时
                    BrandInfo brandInfo = userBrandInfoService.getUserSelectBrandInfo(userInfo.getUserId());
                    if (brandInfo != null) {

                        redisCacheUtil.setCacheObject("ACTIVE_10SEC_MSG_PUSH" + userInfo.getUserId(), 1, 3600);

                        Map<String, String> headMap = Maps.newHashMap();
                        headMap.put("userId", userInfo.getUserId().toString());
                        headMap.put("profile", platformSysInfoConfig.getSysGzh().getGzhCode());

                        String finalReqUrl = platformSysInfoConfig.getTaskUrl() + "call10sec";

                        String wxSpdiderResult = HttpClientUtils.sendPost(finalReqUrl, headMap, Maps.newHashMap());

                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
